package com.china08.yxyapi.api.manage.sch.platform.xiaoben.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonAuth;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonAuthRepository;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class UserAuthUserKechengDetailController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonAuthRepository authRepository;

    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/auth/user/kecheng/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        List<String> userIdList = reqModel.list;

        List<Pojo> returnList = new ArrayList<Pojo>();
        for (String userId : userIdList) {

            Pojo pojo = new Pojo();
            pojo.lessonCount = authRepository.countByUserId(userId);
            YxyLessonsSchoolLessonAuth auth = authRepository.findTop1ByUserIdOrderByCreatedDateDesc(userId);
            if (auth != null) {
                Date date = auth.getCreatedDate();
                if (date != null) {
                    pojo.lastAuthDate = DateToolUtils.format(date, "yyyy-MM-dd HH:mm:ss");
                }
            }

            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.list = returnList;
        return respModel;
    }

    public static class Pojo {
        public Integer lessonCount;
        public String lastAuthDate;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public List<String> list;
    }

    public static class RespModel extends YxyApiRespModel {
        public List<Pojo> list;
    }

}
